/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.zhixun.mobile.modules.wechat.web;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.Option;
import com.boxin.ims.modules.wechat.entity.Question;
import com.boxin.ims.modules.wechat.entity.Questionnaire;
import com.boxin.ims.modules.wechat.entity.QuestionnaireReply;
import com.boxin.ims.modules.wechat.entity.QuestionnaireReplyHuman;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.QuestionService;
import com.boxin.ims.modules.wechat.service.QuestionnaireReplyHumanService;
import com.boxin.ims.modules.wechat.service.QuestionnaireReplyService;
import com.boxin.ims.modules.wechat.service.QuestionnaireService;
import com.boxin.ims.modules.wechat.service.WechatMemberService;

/**
 * 调查问卷回答Controller
 * @author XiaoQian
 * @version 2014-05-09
 */
@Controller
@RequestMapping(value = "/wechat/questionnaireReply")
public class QuestionnaireReplyController extends BaseController {

	@Autowired
	private QuestionnaireReplyService questionnaireReplyService;
	
	@Autowired
	private QuestionnaireService questionnaireService;
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private WechatMemberService wechatMemberService;
	
	@Autowired
	private QuestionnaireReplyHumanService questionnaireReplyHumanService;
	
	@ModelAttribute
	public QuestionnaireReply get(@RequestParam(required=false) Long id) {
		if (id != null){
			return questionnaireReplyService.get(id);
		}else{
			return new QuestionnaireReply();
		}
	}
	
	@RequiresPermissions("wechat:questionnaireReply:view")
	@RequestMapping(value = {"list", ""})
	public String list(QuestionnaireReply questionnaireReply, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
        Page<QuestionnaireReply> page = questionnaireReplyService.find(new Page<QuestionnaireReply>(request, response), questionnaireReply); 
        model.addAttribute("page", page);
		return "modules/wechat/questionnaireReplyList";
	}

	@RequiresPermissions("wechat:questionnaireReply:view")
	@RequestMapping(value = "form")
	public String form(QuestionnaireReply questionnaireReply, Model model) {
	
		User user = UserUtils.getUser();
		
		model.addAttribute("questionnaireReply", questionnaireReply);
		return "modules/wechat/questionnaireReplyForm";
	}

	@RequestMapping(value = "save")
	public String save( Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		String questionnaireId=request.getParameter("questionnaireId");
		String name=request.getParameter("name");
		String mobile=request.getParameter("mobile");
		
		Questionnaire questionnaire= questionnaireService.get(Long.parseLong(questionnaireId));
		
		WechatMember member= wechatMemberService.getByMobile(questionnaire.getUser(), mobile);
		
		List<Question> questionList=questionService.findByQuestionnaireId(Long.parseLong(questionnaireId));
		QuestionnaireReplyHuman replyHuman=new QuestionnaireReplyHuman();
		
		replyHuman.setName(name);
		replyHuman.setMobile(mobile);
		if(member!=null){
			replyHuman.setWechatMember(member);
			replyHuman.setWechatId(member.getWechatId());
		}
		questionnaireReplyHumanService.save(replyHuman);
		for (int i = 0; i < questionList.size(); i++) {
			QuestionnaireReply questionnaireReply =new QuestionnaireReply();
			
			
			questionnaireReply.setReplyHuman(replyHuman);
			
			
			questionnaireReply.setQuestion(questionList.get(i));
			
			
			
			String questionId= questionList.get(i).getId().toString();
			
			String option =request.getParameter("option"+questionId);
			
			
			Set<Option> set=questionList.get(i).getOption();
			Iterator<Option> it = set.iterator();
			
			if(questionList.get(i).getType().equals("0")){
				while(it.hasNext()){
					Option option6= it.next();
					if(option6.getId().toString().equals(option)){
						questionnaireReply.setAnswer(option6.getName());
					}
				}
				
			}else if(questionList.get(i).getType().equals("1")){
				String[] str=request.getParameterValues("option"+questionId);
				String options="";
				while(it.hasNext()){
					Option option5= it.next();
					for (int j = 0; j < str.length; j++) {
						
						if(option5.getId().toString().equals(str[j])){
							options+=option5.getName()+",";
						}
					}
				}
				questionnaireReply.setAnswer(options.substring(0, options.length()-1));
			}else if(questionList.get(i).getType().equals("2")){
				questionnaireReply.setAnswer(option);
			}
			
			questionnaireReplyService.save(questionnaireReply);
		}
		questionnaireReplyHumanService.save(replyHuman);
		
		
		if(member!=null){
			return "modules/member/themes/ShiShangHong/QuestionnaireOk";
		}else{
			return "modules/member/themes/ShiShangHong/QuestionnaireToReg";
		}
	}
	
	@RequiresPermissions("wechat:questionnaireReply:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		questionnaireReplyService.delete(id);
		addMessage(redirectAttributes, "删除调查问卷回答成功");
		return "redirect:"+Global.ADMIN_PATH+"/modules/wechat/questionnaireReply/?repage";
	}
	
	
	@RequestMapping(value = "viewQuestionnaire/{id}")
	public String viewQuestionnaire(@PathVariable Long id, RedirectAttributes redirectAttributes, Model model) {
		
		Questionnaire questionnaire= questionnaireService.get(id);
		
		List<Question> questionList=questionService.findByQuestionnaireId(id);
		
		System.out.println(questionList.size());
		
		model.addAttribute("questionList", questionList);
		model.addAttribute("questionnaire", questionnaire);
		return "modules/member/themes/ShiShangHong/viewQuestionnaire";
	}

}
